import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
from mpl_toolkits.mplot3d import Axes3D
import math
import random
import os
import time
from PSO import PSO
from SSA2 import SSA
from SSA2020 import SSA2020
from SSA2020_test import SSA2020g
from SCA import SCA
from AOA import AOA
from SAOA import SAOA
from MSAOA import MSAOA
from AOA1 import IAOA
from WOA import WOA
from GSA import GSA
from TLBO import TLBO
import obj_funs

detail = {
    'F1': [-100, 100, 2],
    'F2': [-10, 10, 30],
    'F3': [-100, 100, 30],
    'F4': [-10, 10, 30],
    'F5': [-30, 30, 30],
    'F6': [-100, 100, 30],
    'F7': [-1.28, 1.28, 30],
    'F8': [-500, 500, 30],  # 多峰开始
    'F9': [-5.12, 5.12, 30],
    'F10': [-32, 32, 30],
    'F11': [-600, 600, 30],
    'F12': [-50, 50, 30],
    'F13': [-50, 50, 30],
    'F14': [-65, 65, 2],  # 2维
    'F15': [-5, 5, 4],  # 4维
    'F16': [-5, 5, 2],  # 2维
    'F17': [-5, 5, 2],  # 2维
    'F18': [-2, 2, 2],  # 2维
    'F19': [1, 3, 3],  # 3维
    'F20': [0, 1, 6],  # 6维
    'F21': [0, 10, 4],  # 4维
    'F22': [0, 10, 4],  # 4维
    'F23': [0, 10, 4]  # 4维
}


# 画图
def chart(fit_data, box_data):
    plt.figure(num=1, figsize=(9, 7))
    plt.subplot(1, 2, 1)
    plt.xlabel('迭代次数')
    plt.ylabel('适应值')
    plt.ticklabel_format(style='sci', scilimits=(0, 0), axis='y')
    plt.grid(True, linestyle='--', alpha=0.5)
    for i in range(len(fit_data)):
        plt.semilogy(fit_data[i][1], label=fit_data[i][0])  # y轴变为对数刻度
        # plt.plot(fit_data[i][1], label=fit_data[i][0])
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.legend()

    plt.subplot(1, 2, 2)
    labels = ['aoa','msaoa']
    plt.boxplot(box_data, labels=labels)

    '''秩和检验'''
    stats1, p = stats.ranksums(fit_data[0][1], fit_data[1][1])
    print('秩和检验结果', stats1, p)


    plt.show()



# 迭代参数
name = 'F10'
dimensions = detail[name][2]
populationSize = 20
iterations = 300
low = [detail[name][0] for i in range(dimensions)]
up = [detail[name][1] for i in range(dimensions)]
obj_func = getattr(obj_funs, name)

aoa = AOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
aoa_best_fit, aoa_box = aoa.aoa(isBox=True)
msaoa = MSAOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
msaoa_best_fit, msaoa_box = msaoa.msaoa(isBox=True)
# saoa = SAOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# saoa_best_fit,saoa_box = saoa.aoa(isBox=True)
# ssa2020 = SSA2020(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# ssa2020_best_fit, ssa2020_box = ssa2020.Tent_SSA(isBox=True)
# ssa2020g = SSA2020g(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# ssa2020g_best_fit, ssa2020g_box = ssa2020g.Tent_SSA(isBox=True)

fit_data = []
# fit_data.append(['PSO',pso_best_fit])
# fit_data.append(['SSA',ssa_best_fit])
# fit_data.append(['SSA2020',ssa2020_best_fit])
# fit_data.append(['SSA2020g',ssa2020g_best_fit])
# fit_data.append(['SCA',sca_best_fit])
fit_data.append(['AOA', aoa_best_fit])
fit_data.append(['MSAOA', msaoa_best_fit])
# fit_data.append(['SAOA', saoa_best_fit])
# fit_data.append(['MSAOA',msaoa__best_fit])
# fit_data.append(['IAOA', iaoa__best_fit])
# fit_data.append(['WOA',woa__best_fit])
# fit_data.append(['GSA', gsa__best_fit])
# fit_data.append(['TLBO',tlbo__best_fit])

box_data = []
box_data.append(aoa_box[0])
box_data.append(msaoa_box[0])
# box_data.append(saoa_box[0])
# box_data.append(ssa2020_box[0])
# box_data.append(ssa2020g_box[0])
# box_data.append(saoa_box[0])

chart(fit_data, box_data)
